/**
 * ...
 * @author Yog
 */

package com.yff.controls;
import js.Dom;
import js.Dom.HtmlDom;
import com.yff.listeners.DomListener;
class FControl 
{
	public var value(getValue, setValue) : String;
	public var innerHTML(getInnerHTML, setInnerHTML) : String;
	public var id(getId,setId) : String;

	public var childNodes(default, null) : List<FControl> ;
	
	private var _inner : Dynamic;
	public function new(data : Dynamic) 
	{
		_inner = cast js.Lib.document.createElement(getDomType());
		if (data  != null)
		{
			_inner.innerHTML = data ;
		}
	}

	public function addEvent(eventName : String, callBack : Dynamic, args : Dynamic)
	{
		DomListener.add(_inner, eventName, function(e)
		{
			callBack(args);
		});	
	}
	
	public function clear()
	{
		_inner.innerHTML = "";
		if (_inner.childNodes != null)
		{
			var l : Int = _inner.childNodes.length;
			for(i in 0...l)
			{
				_inner.removeChild(_inner.childNodes[i]);
			}
		}
		if (childNodes != null)
			childNodes.clear();
	}

	public function appendChild(control : FControl)
	{
		//if (control.inner != null)
			_inner.appendChild(control._inner);
		if (childNodes == null)
		{
			childNodes = new List<FControl>();
		}
		childNodes.add(control);
	}
	
	private function getInnerHTML():String 
	{
	   return _inner.innerHTML;
	}

	private function setInnerHTML( v : String ) : String
	{
	  _inner.innerHTML = v;
	  return _inner.innerHTML;
	}

	private function getId():String 
	{
	   return _inner.id;
	}

	private function setId( v : String ) : String
	{
	  _inner.id = v;
	  return _inner.id;
	}
	
	private function getValue():String 
	{
	   return _inner.value;
	}

	private function setValue( v : String ) : String
	{
	  _inner.value = v;
	  return _inner.value;
	}	
	
	private function getDomType() : String
	{
		return "";
	}	
}